<template>
  <div class='xtx-goods-page'>
    <div class="container">
      <!-- 面包屑 -->
      <el-breadcrumb :separator-icon="ArrowRight" class="goods-bread" v-if="goods.categories">
        <el-breadcrumb-item class="goods-bread-item" :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="`/category/${goods.categories[1].id}`" class="goods-bread-item" >{{goods.categories[1].name}}</el-breadcrumb-item>
        <el-breadcrumb-item :to="`/category/sub/${goods.categories[0].id}`" class="goods-bread-item" >{{goods.categories[0].name}}</el-breadcrumb-item>
        <el-breadcrumb-item class="animate__animated animate__backInRight">{{goods.name}}</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures"/>
          <GoodsSales />
        </div>
        <div class="spec">
          <GoodsName :goods="goods"/>
          <!-- 规格组件 -->
          <GoodsSku :goods='goods'/>
          <!-- 数量组件 -->
          <div class="goods-count">
            <span>数量</span>
            <el-input-number v-model="num" size="mini" :max="goods.inventory" :min = '0'/>
          </div>
          <!-- 加入購物車 -->
          <XtxButton type="primary" style="margin-top:20px;">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs :goods="goods" />
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <GoodsHot :goodsId="goods.id" :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ArrowRight } from '@element-plus/icons'
import GoodsRelevant from './components/goods-relevant'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsSku from './components/goods-sku'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import GoodsWarn from './components/goods-warn'
import { findGoods } from '@/api/product.js'
import { ref, watch, provide } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsName, GoodsSales, GoodsSku, GoodsTabs,
                GoodsHot, GoodsWarn
  },
  setup () {
    // 根据商品ID获取的数据列表
    const goods = ref({})
    provide('goods',goods)
    const route = useRoute()
    const num = ref(1)

    watch(() => route.params.id, newval => {
    if (route.path === `/product/${newval}`) {
      findGoods(route.params.id).then(res => {
        goods.value = res.result

      })
    }
  },{immediate:true})
    return {
      ArrowRight,
      goods,
      num
    }
  }
}
</script>

<style scoped lang='less'>
:deep(.goods-bread) {
  padding: 20px 0;
  .goods-bread-item {
    color: #333;
    span.el-breadcrumb__inner {
      font-weight: unset;
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
    .goods-count {
      padding-left: 10px;
      padding-top: 20px;
      span {
        color: #999;
        margin-right: 20px;
      }
    }
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  width: 100%;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>